var typeFun = {};
typeFun.renderTreeSelectNode = null;

typeFun.getSelectId = function () {
    if (typeFun.renderTreeSelectNode) {
        return typeFun.renderTreeSelectNode.id
    }
    return 0;
}


typeFun.getSelectName = function () {
    if (typeFun.renderTreeSelectNode) {
        return typeFun.renderTreeSelectNode.title
    }
    return "根节点";
}
typeFun.renderTree = function () {
    $.ajax({
        type: "POST",
        url: getRoot()+'/type/manageTree.action',
        success: function (data) {
            typeFun.initTreeData(data);
        }
    });
}

typeFun.initTreeData = function (data) {
    layui.use(['tree', 'util'], function () {
        var tree = layui.tree;
        var layer = layui.layer
        var util = layui.util
        //按钮事件
        util.event('lay-demo', {
            getChecked: function (othis) {
                var checkedData = tree.getChecked('demoId1'); //获取选中节点的数据

                layer.alert(JSON.stringify(checkedData), {shade: 0});
                console.log(checkedData);
            }
            , setChecked: function () {
                tree.setChecked('demoId1', [12, 16]); //勾选指定节点
            }
            , reload: function () {
                //重载实例
                tree.reload('demoId1', {});

            }
        });

        //仅节点左侧图标控制收缩
        tree.render({
            elem: '#test13'
            , data: data
            , onlyIconControl: true  //是否仅允许节点左侧图标控制展开收缩
            , click: function (obj) {
                typeFun.renderTreeSelectNode = obj.data;
                $("#nowNodeId").html(obj.data.id);
                $("#nowNodeName").html(obj.data.title);
                layui.table.reload('typeReload', {
                    where: {
                        'parentId': typeFun.getSelectId()
                    }
                });

            }
        });

    });
}
//重新加载
typeFun.reload = function () {
    window.location.reload();
}
//重新加载
typeFun.newData = function () {
    layer.open({
        type: 2,
        title: '新增',
        shadeClose: true,
        shade: false,
        maxmin: false, //开启最大化最小化按钮
        area: ['600px', '350px'],
        content: getRoot()+ '/type/loadForm.action',
        success: function (layero, index) {
            //调用子页面的初始化方法
            var $iframeDom = $(layero).find("iframe").eq(0);
            $iframeDom[0].contentWindow.typeFormFun.init(typeFun.dataCallback,typeFun.getSelectId(),typeFun.getSelectName())
        }
    });
}
typeFun.dataCallback = function (value) {
    // console.log(value)
    typeFun.reload();
}


typeFun.renderTable = function () {

    layui.use('table', function () {
        var table = layui.table;
        //监听表格复选框选择
        table.on('checkbox(gridtable)', function (obj) {
            console.log(obj)
        });
        //监听工具条
        table.on('tool(gridtable)', function (obj) {
            var data = obj.data;
            if (obj.event === 'detail') {
                layer.msg('ID：' + data.id + ' 的查看操作');
            } else if (obj.event === 'del') {
                layer.confirm('真的删除行么', function (index) {
                    layer.close(index);
                    $.post(getRoot()+"/type/delete.action?id="+data.id,function(res){
                        layer.msg('操作成功',{time:2000},function(){
                            typeFun.reload();
                        })
                    }).error(function(xhr, status, info){layer.msg('操作失败')});

                });
            } else if (obj.event === 'edit') {
                layer.open({
                    type: 2,
                    title: '编辑',
                    shadeClose: true,
                    shade: false,
                    maxmin: false, //开启最大化最小化按钮
                    area: ['600px', '350px'],
                    content: getRoot()+'/type/loadForm.action?id=' + data.id,
                    success: function (layero, index) {
                        //调用子页面的初始化方法
                        var $iframeDom = $(layero).find("iframe").eq(0);
                        $iframeDom[0].contentWindow.typeFormFun.init(typeFun.dataCallback,typeFun.getSelectId(),typeFun.getSelectName())
                    }
                });
            }
        });


        table.render({
            elem: '#gridtable'
            , url: getRoot()+'/type/manageList.action'
            , page: { //支持传入 laypage 组件的所有参数（某些参数除外，如：jump/elem） - 详见文档
                layout: ['limit', 'count', 'prev', 'page', 'next', 'skip'] //自定义分页布局
                //,curr: 5 //设定初始在第 5 页
                , groups: 1 //只显示 1 个连续页码
                , first: false //不显示首页
                , last: false //不显示尾页

            }
            , where: {
                'parentId': typeFun.getSelectId()
            }
            , cols: [[
                {field: 'id', width: 200, title: '编码'}
                , {field: 'name', width: 300, title: '名称'}
                , {field: 'seqNum', width: 300, title: '排序'}
                , {fixed: 'right', width: 300, title: '操作', align: 'center', toolbar: '#barDemo'}
            ]]
            ,id: 'typeReload'

        });
    });
}

$(function () {
    typeFun.renderTree();
    typeFun.renderTable();
    $("#addBtn").click(typeFun.newData);
});